﻿Date.prototype.format = function(format){
	if(!format){
		throw "Invalid argument format";
	}

	format = (typeof format === "object" ? format.toString() : format + "");

	var $this = this,
		map = {
		    "M": this.getMonth() + 1, //月份 
		    "d": this.getDate(), //日 
		    "h": this.getHours(), //小时 
		    "m": this.getMinutes(), //分 
		    "s": this.getSeconds(), //秒 
		    "q": Math.floor((this.getMonth() + 3) / 3), //季度 
		    "S": this.getMilliseconds() //毫秒 
		};

	format = format.replace(/([yMdhmsqS])+/g, function(all, t){
	    var v = map[t];
	    if(v !== undefined){
	        if(all.length > 1){
	            v = "0" + v;
	           	v = v.substr(v.length-2);
	        }

	        return v;
	    }else if(t === "y"){
	        return ($this.getFullYear() + '').substr(4 - all.length);
	    }

	    return all;
	});

	return format;
}

// 彩票开奖配置
exports.cp = [
	/* 重庆时时彩开始 */
	/*{
		title: '重庆时时彩',
		source: '百度彩票',
		name: 'cqssc',
		enable: true,
		timer: 'cqssc',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=200',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase.substr(0, 8) + "-" + data.phase.substr(8, 3);
					return {type: 1, time: data.time_draw, number: number, data: data.result.result[0].data.join(",")};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票重庆时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},*/

	{
		title: '重庆时时彩',
		source: '开彩网',
		name: 'cqssc',
		enable: true,
		timer: 'cqssc',
		option: {
			host: "d.apiplus.net",
			timeout: 50000,
			path: '/newly.do?token=14c15408585046d2&code=cqssc&rows=1&format=json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://d.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 1, function(data){
					return data.expect.substr(0, 8) + "-" + data.expect.substr(8, 3);
				}, '/newly.do?token=14c15408585046d2&code=cqssc&rows=1&format=json');
			}catch(e){
				throw('--------开彩网重庆时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	/*{
		title: '重庆时时彩',
		source: '开彩网',
		name: 'cqssc',
		enable: true,
		timer: 'cqssc',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/cqssc-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 1, function(data){
					return data.expect.substr(0, 8) + "-" + data.expect.substr(8, 3);
				}, '/cqssc-1.json');
			}catch(e){
				throw('--------开彩网重庆时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},*/
	/* 重庆时时彩结束 */

	/* 新疆时时彩开始 */
	{
		title: '新疆时时彩',
		source: '500彩票网',
		name: 'xjssc',
		enable: true,
		timer: 'xjssc',
		option: {
			host: "kaijiang.500.com",
			timeout: 50000,
			path: '/static/info/kaijiang/xml/xjssc/' + (new Date()).format("yyyy-MM-dd").replace(/-/g, "") + '.xml',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://kaijiang.500.com/"
			}
		},
		parse: function(str){
			try{
				var pattern = /<row expect="([\d\-]+?)" opencode="([\d\,]+?)" specail="[^"]*" opentime="([\d\:\- ]+?)"/,
					m = "";

				str = str.substr(0, 220);

				if(m = str.match(pattern)){
					var number = m[1].substr(0, 8) + "-" + m[1].substr(9, 2);
					return {type: 12, time: m[3], number: number, data: m[2]};
				}else{
					return null;
				}
			}catch(e){
				throw('--------500彩票网新疆时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '新疆时时彩',
		source: '开彩网',
		name: 'xjssc',
		enable: true,
		timer: 'xjssc',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/xjssc-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 12, function(data){
					return data.expect.substr(0, 8) + "-" + data.expect.substr(9, 3);
				}, '/xjssc-1.json');
			}catch(e){
				throw('--------开彩网新疆时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 新疆时时彩结束 */

	/*天津时时彩开始 */
	{
		title: '天津时时彩',
		source: '500彩票网',
		name: 'tjssc',
		enable: true,
		timer: 'tjssc',
		option: {
			host: "kaijiang.500.com",
			timeout: 50000,
			path: '/static/info/kaijiang/xml/tjssc/' + (new Date()).format("yyyy-MM-dd").replace(/-/g, "") + '.xml',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://kaijiang.500.com/"
			}
		},
		parse: function(str){
			try{
				var pattern = /<row expect="([\d\-]+?)" opencode="([\d\,]+?)" specail="[^"]*" opentime="([\d\:\- ]+?)"/,
					m = "";

				str = str.substr(0, 220);

				if(m = str.match(pattern)){
					var number = m[1].substr(2);
					return {type: 60, time: m[3], number: number, data: m[2]};
				}else{
					return null;
				}
			}catch(e){
				throw('--------500彩票网天津时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '天津时时彩',
		source: '开彩网',
		name: 'tjssc',
		enable: true,
		timer: 'tjssc',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/tjssc-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 60, function(data){
					return data.expect.substr(2);
				}, '/tjssc-1.json');
			}catch(e){
				throw('--------开彩网天津时时彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 天津时时彩结束 */

	/* 广东11选5开始 */
	/*{
		title: '广东11选5',
		source: '百度彩票',
		name: 'gd11',
		enable: true,
		timer: 'gd11',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=23',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase;
					return {type: 6, time: data.time_draw, number: number, data: data.result.result[0].data.join(",")};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票广东11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},*/

	{
		title: '广东11选5',
		source: '开彩网',
		name: 'gd11',
		enable: true,
		timer: 'gd11',
		option: {
			host: "d.apiplus.net",
			timeout: 50000,
			path: '/newly.do?token=14c15408585046d2&code=gd11x5&rows=1&format=json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://d.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 6, function(data){
					return data.expect.substr(2);
				}, '/newly.do?token=14c15408585046d2&code=gd11x5&rows=1&format=json');
			}catch(e){
				throw('--------开彩网广东11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '广东11选5',
		source: '开彩网',
		name: 'gd11',
		enable: true,
		timer: 'gd11',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/gd11x5-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 6, function(data){
					return data.expect.substr(2);
				}, '/gd11x5-1.json');
			}catch(e){
				throw('--------开彩网广东11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 广东11选5结束 */

	/* 上海11选5开始 */
	{
		title: '上海11选5',
		source: '百度彩票',
		name: 'sh11',
		enable: true,
		timer: 'sh11',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=562',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase.substr(2);
					return {type: 15, time: data.time_draw, number: number, data: data.result.result[0].data.join(",")};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票上海11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '上海11选5',
		source: '开彩网',
		name: 'sh11',
		enable: true,
		timer: 'sh11',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/sh11x5-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 15, function(data){
					return data.expect.substr(2);
				}, '/sh11x5-1.json');
			}catch(e){
				throw('--------开彩网上海11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 上海11选5结束 */

	/* 江西11选5开始 */
	{
		title: '江西11选5',
		source: '500彩票网',
		name: 'jx11',
		enable: true,
		timer: 'jx11',
		option: {
			host: "kaijiang.500.com",
			timeout: 50000,
			path: '/static/info/kaijiang/xml/dlc/' + (new Date()).format("yyyy-MM-dd").replace(/-/g, "") + '.xml',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://kaijiang.500.com/"
			}
		},
		parse: function(str){
			try{
				var pattern = /<row expect="([\d\-]+?)" opencode="([\d\,]+?)" specail="[^"]*" opentime="([\d\:\- ]+?)"/,
					m = "";

				str = str.substr(0, 220);

				if(m = str.match(pattern)){
					var number = "20" + m[1];
					return {type: 16, time: m[3], number: number, data: m[2]};
				}else{
					return null;
				}
			}catch(e){
				throw('--------500彩票网江西11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '江西11选5',
		source: '开彩网',
		name: 'jx11',
		enable: true,
		timer: 'jx11',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/jx11x5-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 16, function(data){
					return data.expect.substr(0, 8) + "-" + data.expect.substr(9, 3);
				}, '/jx11x5-1.json');
			}catch(e){
				throw('--------开彩网江西11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 江西11选5结束 */

	/* 山东11选5开始 */
	{
		title: '山东11选5',
		source: '开彩网',
		name: 'jx11',
		enable: true,
		timer: 'jx11',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/sd11x5-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 7, function(data){
					return data.expect.substr(2);
				}, '/sd11x5-1.json');
			}catch(e){
				throw('--------开彩网山东11选5解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 山东11选5结束 */

	/* 排列三开始 */
	{
		title: '排列三',
		source: '百度彩票',
		name: 'pl3',
		enable: true,
		timer: 'pl3',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=3',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = "20" + data.phase;
					return {type: 10, time: data.time_draw, number: number, data: data.result.result[0].data.join(",")};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票排列三解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},

	{
		title: '排列三',
		source: '开彩网',
		name: 'pl3',
		enable: true,
		timer: 'pl3',
		option: {
			host: "f.apiplus.net",
			timeout: 50000,
			path: '/pl3-1.json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://f.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 10, function(data){
					return data.expect;
				}, '/pl3-1.json');
			}catch(e){
				throw('--------开彩网排列三解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 排列三结束 */

	/* 福彩3D开始 */
	{
		title: '福彩3D',
		source: '百度彩票',
		name: 'fc3d',
		enable: true,
		timer: 'fc3d',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=52',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase;
					return {type: 9, time: data.time_draw, number: number, data: data.result.result[0].data.join(",")};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票福彩3D解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 福彩3D结束 */

	/* 双色球开始 */
	{
		title: '双色球',
		source: '百度彩票',
		name: 'shuangseqiu',
		enable: true,
		timer: 'shuangseqiu',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=50',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase,
						kjData = data.result.result[0].data.join(",") + "|" + data.result.result[1].data.join(",");
					return {type: 80, time: data.time_draw, number: number, data: kjData};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票双色球解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 双色球结束 */

	/* 大乐透开始 */
	{
		title: '大乐透',
		source: '百度彩票',
		name: 'daletou',
		enable: true,
		timer: 'daletou',
		option: {
			schema: "https",
			host: "caipiao.baidu.com",
			timeout: 50000,
			path: '//lottery/ajax_latestdrawn.php?lottery_type=1',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "https://caipiao.baidu.com/"
			}
		},
		parse: function(str){
			try{
				var data = JSON.parse(str);

				if(data && data.code == 0){
					data = data.data[0];
					var number = data.phase,
						kjData = data.result.result[0].data.join(",") + "|" + data.result.result[1].data.join(",");
					return {type: 81, time: data.time_draw, number: number, data: kjData};
				}else{
					return null;
				}
			}catch(e){
				throw('--------百度彩票大乐透解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 大乐透结束 */

	/* 江苏快三开始 */
	{
		title: '江苏快三',
		source: '500彩票网',
		name: 'jx11',
		enable: true,
		timer: 'jx11',
		option: {
			host: "kaijiang.500.com",
			timeout: 50000,
			path: '/static/info/kaijiang/xml/jsk3/' + (new Date()).format("yyyy-MM-dd").replace(/-/g, "") + '.xml',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://kaijiang.500.com/"
			}
		},
		parse: function(str){
			try{
				var pattern = /<row expect="([\d\-]+?)" opencode="([\d\,]+?)" specail="[^"]*" opentime="([\d\:\- ]+?)"/,
					m = "";

				str = str.substr(0, 220);

				if(m = str.match(pattern)){
					var number = "20" + m[1].substr(0, 6) + m[1].substr(7, 2);
					return {type: 79, time: m[3], number: number, data: m[2]};
				}else{
					return null;
				}
			}catch(e){
				throw('--------500彩票网江苏快三解析数据不正确：' + e + '，数据：' + str);
			}
		}
	},
	/* 江苏快三结束 */

	/* 河内5分彩开始 */
	{
		title: '河内5分彩',
		source: '开彩网',
		name: 'hn5fc',
		enable: true,
		timer: 'hn5fc',
		option: {
			host: "d.apiplus.net",
			timeout: 50000,
			path: '/newly.do?token=14c15408585046d2&code=viffc5&rows=1&format=json',
			headers: {
				"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",
				"Referer": "http://d.apiplus.net/"
			}
		},
		parse: function(str){
			try{
				return kaicaiwang_parse(str, 14, function(data){
					return data.expect.substr(0, 8) + "-" + data.expect.substr(8, 3);
				}, '/newly.do?token=14c15408585046d2&code=viffc5&rows=1&format=json');
			}catch(e){
				throw('--------开彩网河内5分彩解析数据不正确：' + e + '，数据：' + str);
			}
		}
	}
	/* 河内5分彩结束 */
];

// 出错时等待 15                                                                                                
exports.errorSleepTime = 10;                                                                                      

// 重启时间间隔，以小时为单位，0为不重启
//exports.restartTime=0.4;
exports.restartTime = 0;

exports.submit={
	host:'localhost',
	path:'/index.php/dataSource/kj'
}

exports.dbinfo={
	host:'127.0.0.1',
	user:'caipiao',
	password:'caipiao',
	database:'caipiao',
	prefix:'caipiao_'
}

global.log=function(log){
	var date=new Date();
	console.log('['+date.toDateString() +' '+ date.toLocaleTimeString()+'] '+log)
}

function kaicaiwang_parse(str, type, number_callback, url){
	var data = JSON.parse(str);

	if(data && data.data){
		data = data.data[0];

		var number = number_callback(data),
			logData = "type=>" + type + ", url=>" + url + ", str=>" + str + ", number=>" + number + ", opentime=>" + data.opentime + ", code: " + data.opencode;
		log("kaicaiwang data parse: " + logData);
		return {type: type, time: data.opentime, number: number, data: data.opencode};
	}else{
		return null;
	}
}